﻿var grid, cbProgress;
var itemgrid;

//初始化主表
function InitGridMain() {
    var GoodsMoveID = $.trim($("#txtGoodsMoveID").attr("value"));
    var CheckTimeS = $('#CheckTimeS').datebox('getValue');
    var CheckTimeE = $('#CheckTimeE').datebox('getValue');
    var SGoodsCode = $.trim($("#txtSGoodsCode").attr("value"));
    var SGoodsName = $.trim($("#txtSGoodsName").attr("value"));
    grid = $('#tab_list').datagrid({
        name: 'tbl_data',
        url: '/GoodsMove/QueryMain',
        title: '商品货位搬移单列表',
        queryParams: {
            GoodsMoveID: GoodsMoveID,
            CheckTimeS: CheckTimeS,
            CheckTimeE: CheckTimeE,
            SGoodsCode: SGoodsCode,
            SGoodsName: SGoodsName
        },
        //width: 815,
        height: 700,
        //border : false,
        //fit: false,
        fitColumns: false,
        //nowrap: true,
        rownumbers: true,
        //showFooter: true,
        //loadMsg: '正在加载信息...',
        //frozen: true,
        //sortName: "CODE", //排序设置
        singleSelect: true,
        //pagination: true,
        //pageSize: 5,
        //pageNumber: 1,
        //pageList: [5,10, 20, 30, 40, 50],
        idField: 'GOODSMOVEID',
        columns: [[
        //{ field: 'ck', checkbox: true },
                    //{ field: 'GoodsID', title: '商品ID', hidden: true },
                    { field: 'GOODSMOVEID', title: '商品货位搬移单号', width: 117, align: 'center', sortable: true },
                    { field: 'MOVEDATE', title: '搬移日期', width: 150, align: 'center', sortable: true, formatter: ChangeDateFormat },
                    { field: 'MOVETYPE', title: '搬移类型', width: 70, align: 'center', sortable: true },
                    { field: 'WAREHOUSENAME', title: '仓库', width: 70, align: 'center', sortable: true },
                    { field: 'CELLNO', title: '库位', width: 70, align: 'center', sortable: true },
                    { field: 'FLAG', title: '状态', width: 150, align: 'center', sortable: true, formatter: formatflag },
                    { field: 'CREATOR', title: '创建人', width: 100, align: 'center', sortable: true },
                    { field: 'CREATEDATE', title: '创建时间', width: 100, align: 'center', sortable: true, formatter: ChangeDateFormat },
                    { field: 'EDITOR', title: '修改人', width: 150, align: 'center', sortable: true },
                    { field: 'EDITDATE', title: '修改时间', width: 100, align: 'center', sortable: true, formatter: ChangeDateFormat },
                    { field: 'REMARK', title: '备注', width: 150, align: 'center', sortable: true },
                    { field: 'WAREHOUSEID', title: '操作', width: 117, align: 'center', sortable: true, formatter: formataudit }

        ]],
        toolbar: [{
            id: 'btnAdd',
            text: '新增',
            iconCls: 'icon-add',
            handler: function () {
                add();
            }
        }, '-', {
            id: 'btnEdit',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                edit();
            }
        }, '-', {
            id: 'btnRemove',
            text: '删除',
            iconCls: 'icon-remove',
            handler: function () {
                del();
            }
        }, '-', {
            id: 'btnShowItem',
            text: '明细修改',
            iconCls: 'icon-edit',
            handler: function () {
                showItem();
            }
        }],
        pagination: true,
        pageSize: 20,
        pageNumber: 1,
        pageList: [10, 20, 30, 40, 50],
        rownumbers: true, //行号
        onLoadSuccess: function (data) {
            onRefreshButton();
        },
        onSelect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onUnselect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onSortColumn: function (sort, order) {
            grid.datagrid('reload');
        }
    });
    //清空选择
    grid.datagrid('clearSelections');
}

$(function () {
    $('#tab_list').datagrid({
        view: detailview,
        detailFormatter: function (index, row) {
            return '<div style="padding:2px"><table class="ddv"></table></div>';
        },
        onExpandRow: function (index, row) {
            $('#tab_list').datagrid("loading", "正在检索数据...");
            var ddv = $(this).datagrid('getRowDetail', index).find('table.ddv');
            ddv.datagrid({
                url: '/GoodsMove/QueryDetailData' + '?GOODSMOVEID=' + row.GOODSMOVEID,
                fitColumns: true,
                singleSelect: true,
                rownumbers: true,
                loadMsg: '',
                height: 'auto',
                columns: [[
                    { field: 'GOODSMOVEID', title: '商品货位搬移单号', align: 'center', sortable: true },
                    { field: 'GOODSBATCHCODE', title: '商品批次号', align: 'center', sortable: true },
                    { field: 'FROMLOCATIONCODE', title: '来源货位', align: 'center', sortable: true },
                    { field: 'TOLOCATIONCODE', title: '目的货位', align: 'center', sortable: true },
                    { field: 'GOODSCODE', title: '商品编码', align: 'center', sortable: true },
                    { field: 'GOODSNAME', title: '商品名称', align: 'center', sortable: true },
                    { field: 'QTY', title: '数量', align: 'center', sortable: true },
                    { field: 'OFFSHELVESID', title: '下架指令ID', align: 'center', sortable: true }
                ]],
                onResize: function () {
                    $('#tab_list').datagrid('fixDetailRowHeight', index);
                },
                onLoadSuccess: function () {
                    $('#tab_list').datagrid("loaded");
                    setTimeout(function () {
                        grid.datagrid('fixDetailRowHeight', index);
                    }, 0);
                },
                loadFilter: function (data) {
                    if (data.ResponseData) {
                        return data.ResponseData;
                    } else {
                        return data;
                    }
                }
            });
            $('#tab_list').datagrid('fixDetailRowHeight', index);
        }
    });
});

//控制按钮
function onRefreshButton() {
    $('#btnAdd').linkbutton('enable');
    $('#btnEdit').linkbutton('disable');
    $('#btnRemove').linkbutton('disable');
    $('#btnShowItem').linkbutton('disable');

    //$('#btnImport').linkbutton('disable');
    // $('#btnExport').linkbutton('disable');

    var rows = grid.datagrid('getSelections');
    if (rows.length == 1) {
        $('#btnEdit').linkbutton('enable');
        $('#btnRemove').linkbutton('enable');
        $('#btnShowItem').linkbutton('enable');
    }
}


function ChangeDateFormat(val) {
    if (val != null) {
        var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
        //月份为0-11，所以+1，月份小于10时补个0
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        return date.getFullYear() + "-" + month + "-" + currentDate;
    }

    return "";
}

function formatflag(val) {
    if (val != null) {
        if (val == 0) {
            return "未处理";
        }
        if (val == 40) {
            return "在执行";
        }
        if (val == 100) {
            return "完结";
        }
    }
}

//增加
function add() {
    $('#p1').dialog('open').dialog('setTitle', '新增商品货位搬移主表信息');
    $("#hiddenGOODSMOVEID").val("");
    $('#MoveType').combobox("setValue", "ON");
    $("#txtRemark").val("");
    $('#WAREHOUSEID').combogrid("setValue", "");
    $('#CELLNO').combogrid("setValue", "");
    $('#Flag').combobox("setValue", "0");
    url = '/GoodsMove/Saves';
}

//修改
function edit() {
    var rows = grid.datagrid('getSelections');
    $('#p1').dialog('open').dialog('setTitle', '修改商品货位搬移主表信息');
    $("#hiddenGOODSMOVEID").val(rows[0].GOODSMOVEID);
    $('#MoveType').combobox("setValue", rows[0].MOVETYPE);
    $("#MoveDate").datebox("setValue", rows[0].MOVEDATE);
    $("#txtRemark").val(rows[0].REMARK);
    $('#CellNo').combogrid("setValue", rows[0].CELLNO);
    $('#WAREHOUSEID').combogrid("setValue", rows[0].WAREHOUSEID);
    $('#Flag').combobox("setValue", rows[0].FLAG);
    url = '/GoodsMove/Saves';
}



//删除
function del() {
    var ids = [];
    var rows = grid.datagrid('getSelections');
    for (var i = 0; i < rows.length; i++) {
        ids.push(rows[i].GOODSMOVEID);
    }
    $.messager.confirm('提示信息', '您确认要删除吗?', function (data) {
        if (data) {
            $('#location').form('submit', {
                url: '/GoodsMove/Delete' + '?ids=' + ids.join(','),
                success: function (data) {
                    eval('data=' + data);
                    if (data.success) {
                        InitGridMain();
                    } else {
                        $.messager.alert('错误', data.msg, 'error');
                    }
                }
            })

        }
    });
}

function save() {
    var GOODSMOVEID = $.trim($("#hiddenGOODSMOVEID").attr("value"));
    var MoveType = $.trim($("#MoveType").attr("value"));
    var MoveDate = $("#MoveDate").datetimebox('getValue');
    var wid = $('#WAREHOUSEID').combobox('getValue');
    var CellNo = $('#CellNo').combobox('getValue');
    var Flag = $('#Flag').combobox('getValue');

    if (MoveType == "") {
        $.messager.alert('提示', '搬移类型不能为空', 'warning');
        $("#MoveType").focus();
        return false;
    }
    if (MoveDate == "") {
        $.messager.alert('提示', '搬移日期不能为空', 'warning');
        $("#MoveDate").focus();
        return false;
    }
    if (wid == null || wid == "") {
        $.messager.alert('提示', '请选择仓库', 'warning');
        $("#WAREHOUSEID").focus();
        return false;
    } if (CellNo == "") {
        $.messager.alert('提示', '库位不能为空', 'warning');
        $("#CellNo").focus();
        return false;
    }
    if (Flag == "") {
        $.messager.alert('提示', '状态不能为空', 'warning');
        $("#Flag").focus();
        return false;
    }


    $('#location').form('submit', {
        url: url + '?GOODSMOVEID=' + GOODSMOVEID,
        success: function (data) {
            eval('data=' + data);
            if (data.success) {
                $('#p1').dialog('close');
                InitGridMain();
            } else {
                $.messager.alert('错误', data.msg, 'error');
                //alert("保存失败！请检查");
            }
        }
    });
}

function showItem() {
    var rows = grid.datagrid('getSelections');
    $('#p4').dialog('open').dialog('setTitle', '搬移明细信息');
    $("#hiddenMOVEID").val(rows[0].GOODSMOVEID);
    InitGrid();
}

function InitGrid() {
    var GoodsMoveID = $.trim($("#hiddenMOVEID").attr("value"));
    itemgrid = $('#tab_itemlist').datagrid({
        name: 'tbl_itemdata',
        url: '/GoodsMove/QueryGoodsMoveItem',
        title: '商品货位搬移单明细列表',
        queryParams: {
            GoodsMoveID: GoodsMoveID
        },
        //width: 815,
        height: 530,
        //border : false,
        //fit: false,
        fitColumns: false,
        //nowrap: true,
        rownumbers: true,
        //showFooter: true,
        //loadMsg: '正在加载信息...',
        //frozen: true,
        //sortName: "CODE", //排序设置
        singleSelect: true,
        idField: 'SEQNO',
        columns: [[
        //{ field: 'ck', checkbox: true },
                    { field: 'SEQNO', title: '明细序列ID', hidden: true },
                    { field: 'GOODSMOVEID', title: '商品货位搬移单号', align: 'center', sortable: true },
                    { field: 'GOODSBATCHCODE', title: '商品批次号', align: 'center', sortable: true },
                    { field: 'FROMLOCATIONCODE', title: '来源货位', align: 'center', sortable: true },
                    { field: 'TOLOCATIONCODE', title: '目的货位', align: 'center', sortable: true },
                    { field: 'GOODSCODE', title: '商品编码', align: 'center', sortable: true },
                    { field: 'GOODSNAME', title: '商品名称', align: 'center', sortable: true },
                    { field: 'QTY', title: '数量', align: 'center', sortable: true },
                    { field: 'OFFSHELVESID', title: '下架指令ID', align: 'center', sortable: true }
        ]],
        toolbar: [{
            id: 'btnAddItem',
            text: '新增',
            iconCls: 'icon-add',
            handler: function () {
                addItem();
            }
        }, '-', {
            id: 'btnEditItem',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                editItem();
            }
        }, '-', {
            id: 'btnRemoveItem',
            text: '删除',
            iconCls: 'icon-remove',
            handler: function () {
                delItem();
            }
        }],
        pagination: true,
        pageSize: 20,
        pageNumber: 1,
        pageList: [10, 20, 30, 40, 50],
        rownumbers: true, //行号
        onLoadSuccess: function (data) {
            onRefreshButtonItem();
        },
        onSelect: function (rowIndex, rowData) {
            onRefreshButtonItem();
        },
        onUnselect: function (rowIndex, rowData) {
            onRefreshButtonItem();
        },
        onSortColumn: function (sort, order) {
            itemgrid.datagrid('reload');
        }
    });
    //清空选择
    itemgrid.datagrid('clearSelections');
}


//增加
function addItem() {
    $('#p3').dialog('open').dialog('setTitle', '新增商品货位搬移明细信息');
    $("#hiddenSEQNO").val("");
    var GoodsMoveID = $.trim($("#hiddenMOVEID").attr("value"));
    $("#GoodsMoveID").val(GoodsMoveID);
    $("#txtGoodsBatchCode").val("");
    $("#txtFromLocationCode").val("");
    $("#txtToLocationCode").val("");
    $("#txtQty").val("");
    $("#txtOffShelvesID").val("");
    $("#txtGoodsCode").val("");
    url = '/GoodsMove/SaveItems';
}

//修改
function editItem() {
    var rows = itemgrid.datagrid('getSelections');
    $('#p3').dialog('open').dialog('setTitle', '修改商品货位搬移明细信息');
    $("#hiddenSEQNO").val(rows[0].SEQNO);
    $("#GoodsMoveID").val(rows[0].GOODSMOVEID);
    $("#txtGoodsBatchCode").val(rows[0].GOODSBATCHCODE);
    $("#txtFromLocationCode").val(rows[0].FROMLOCATIONCODE);
    $("#txtToLocationCode").val(rows[0].TOLOCATIONCODE);
    $("#txtQty").val(rows[0].QTY);
    $("#txtOffShelvesID").val(rows[0].OFFSHELVESID);
    $("#txtGoodsCode").val(rows[0].GOODSCODE);
    url = '/GoodsMove/SaveItems';

}

//删除
function delItem() {
    var ids = [];
    var rows = itemgrid.datagrid('getSelections');
    for (var i = 0; i < rows.length; i++) {
        ids.push(rows[i].SEQNO);
    }
    $.messager.confirm('提示信息', '您确认要删除吗?', function (data) {
        if (data) {
            $('#item').form('submit', {
                url: '/GoodsMove/DeleteItem' + '?ids=' + ids.join(','),
                success: function (data) {
                    eval('data=' + data);
                    if (data.success) {
                        InitGrid();
                    } else {
                        $.messager.alert('错误', data.msg, 'error');
                    }
                }
            })

        }
    });
}


function saveItem() {
    var SEQNO = $.trim($("#hiddenSEQNO").attr("value"));
    var GoodsMoveID = $.trim($("#hiddenMOVEID").attr("value"));
    var GoodsBatchCode = $.trim($("#txtGoodsBatchCode").attr("value"));
    var FromLocationCode = $.trim($("#txtFromLocationCode").attr("value"));
    var ToLocationCode = $.trim($("#txtToLocationCode").attr("value"));
    var QTY = $.trim($("#txtQty").attr("value"));
    var GoodsCode = $.trim($("#txtGoodsCode").attr("value"));

    if (GoodsBatchCode == "") {
        $.messager.alert('提示', '商品批次号不能为空', 'warning');
        $("#txtGoodsBatchCode").focus();
        return false;
    }
    if (FromLocationCode == "") {
        $.messager.alert('提示', '来源货位不能为空', 'warning');
        $("#txtFromLocationCode").focus();
        return false;
    }
    if (ToLocationCode == "") {
        $.messager.alert('提示', '目的货位不能为空', 'warning');
        $("#txtToLocationCode").focus();
        return false;
    }
    if (QTY == "") {
        $.messager.alert('提示', '数量不能为空', 'warning');
        $("#txtQty").focus();
        return false;
    }
    if (GoodsCode == "") {
        $.messager.alert('提示', '商品不能为空', 'warning');
        $("#txtGoodsCode").focus();
        return false;
    }


    $('#item').form('submit', {
        url: url + '?SEQNO=' + SEQNO + '&GoodsMoveID=' + GoodsMoveID,
        success: function (data) {
            eval('data=' + data);
            if (data.success) {
                $('#p3').dialog('close');
                InitGrid();
            } else {
                $.messager.alert('错误', data.msg, 'error');
                //alert("保存失败！请检查");
            }
        }
    });
}

//控制按钮
function onRefreshButtonItem() {
    $('#btnAddItem').linkbutton('enable');
    $('#btnEditItem').linkbutton('disable');
    $('#btnRemoveItem').linkbutton('disable');

    //$('#btnImport').linkbutton('disable');
    // $('#btnExport').linkbutton('disable');

    var rows = itemgrid.datagrid('getSelections');
    if (rows.length == 1) {
        $('#btnEditItem').linkbutton('enable');
        $('#btnRemoveItem').linkbutton('enable');
    }
}

function formataudit(val, row, index) {
    if (val != null) {
        var flag = row.FLAG;
        var GoodsMoveID = row.GOODSMOVEID;
        if (flag != 100) {
            return "<a href='/GoodsMove/Audit?GoodsMoveID=" + GoodsMoveID + "'>审核</a>";
        }
        else {
            return "已审核";
        }
    }
}

